/**
 * 
 * @author Sunix
 * @date   2013-5-24
 * @Email  SunixSky@gmail.com
 * 
 */	
package{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.utils.ByteArray;
	
	import br.com.stimuli.loading.BulkLoader;
	import br.com.stimuli.loading.loadingtypes.LoadingItem;
	
	import business.scene.SceneManager;
	
	import core.General;
	import core.Layer;
	
	import sunix.animationPlayer.AnimationPlayer;
	import sunix.animationPlayer.AnimationPlayerManager;
	import sunix.animationPlayer.data.ActionFrameData;
	import sunix.animationPlayer.data.DirectionFrameData;
	import sunix.animationPlayer.data.FrameData;
	import sunix.animationPlayer.data.RoleAnimationData;
	
	public class MT extends Sprite{
		
		protected var player:AnimationPlayer;
		protected var loader:BulkLoader; 
		protected var show:String = "P005_Su_M_Arc";
		protected var roleAnimationData:RoleAnimationData;
		
		public function MT(){
			super();
			
			// 支持 autoOrient
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			Layer.init(this);
			
			General.loader = new BulkLoader("Game");
			loader = General.loader;

			
			
			
			SceneManager.instance.loadScene();
			
			
//			var avatar:BaseAvatar = new BaseAvatar(1);
//			
//			addChild(avatar);
			AnimationPlayerManager.instance.init(30);
			
			addEventListener(Event.ENTER_FRAME,onEnterFrame);
			
			viewInit();
			initFrames();
		}
		
		protected function onEnterFrame(event:Event):void{
			AnimationPlayerManager.instance.enterFrame();
		}
		
		protected function viewInit():void{
			player = AnimationPlayerManager.instance.getAnimationPlayer();
			player.x = 100;
			player.y = 100;
			addChild(player);
		}
		
		private function initFrames():void{
			//loader = new BulkLoader("Game");
			loader.add("assets/" + show + ".swf",{id:show});
			loader.add("assets/test.jpeg",{id:"test"});
			loader.addEventListener(BulkLoader.COMPLETE,onComplete);
			loader.start();
		}
		
		private function onComplete(event:Event):void{
			var loader:BulkLoader = event.target as BulkLoader;
			var loadingItem:LoadingItem = loader.get(show);
			var xmlClass:Class = loadingItem.getDefinition(show+"Des");
			var ba:ByteArray = new xmlClass();
			var abs:String=ba.readUTFBytes(ba.length);
			var des:XML=XML(abs);
			var bmd:BitmapData = loadingItem.getBitmapData(show+"Res");
			roleAnimationData = parse(show,bmd,des);
			
			var bm:Bitmap = loader.getBitmap("test");
			addChildAt(bm,0);
			test();
		}
		
		private function test():void{
			var actionFrameData:ActionFrameData = roleAnimationData.getActionFrameData("S1");
			var directionFrameData:DirectionFrameData = actionFrameData.getDirectionFrames("090");
			player.frames = directionFrameData.frames;
			player.fps = directionFrameData.fps;
			player.play();
		}
		
		public function parse(resKey:String,bmd:BitmapData,des:XML):RoleAnimationData{
			var roleAnimationData:RoleAnimationData = new RoleAnimationData();
			for each(var xml:XML in des.action){
				var actionFrameData:ActionFrameData = new ActionFrameData();
				var action:String = xml.@name;
				for each(var frames:XML in xml.frames){
					var directFrameData:DirectionFrameData = new DirectionFrameData();
					var direction:String = frames.@direction;
					directFrameData.fps = uint(frames.@fps);
					for each(var xmlData:XML in frames.frame){
						var frameData:FrameData = new FrameData();
						var point:Point = new Point(xmlData.@x,xmlData.@y);
						var width:uint = xmlData.@w;
						var height:uint = xmlData.@h;
						frameData.offsetPoint = new Point(xmlData.@ox,xmlData.@oy);
						frameData.bitmapData = new BitmapData(width,height,true,0x00000000);
						frameData.bitmapData.draw(bmd,new Matrix(1,0,0,1,-point.x,-point.y));
						directFrameData.addFrameData(frameData);
					}
					actionFrameData.addDirectionFrames(direction,directFrameData);
				}
				roleAnimationData.addActionFrameData(action,actionFrameData);
			}
			return roleAnimationData;
		}
	}
}